import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        String str1 = in.next();
        String str2 = in.next();
        in.nextLine();
        String[] strs = new String[n];
        for (int i = 0; i < n; i++) {
            strs[i] = in.nextLine();
        }
        int minLength = 0x3f3f3f3f;
        int str1Index = -1;
        int str2Index = -1;
        for (int i = 0; i < n; i++) {
            if (strs[i].equals(str1)) {
                str1Index = i;
            } else if (strs[i].equals(str2)) {
                str2Index = i;
            }
            if (str1Index != -1 && str2Index != -1) {
                minLength = Math.min(minLength, Math.abs(str1Index-str2Index));
            }
        }
        System.out.println(minLength == 0x3f3f3f3f ? -1 : minLength);
    }
}
